package com.gr100.biz.app.mvc;

import com.gr100.biz.api.order.OrderService;
import com.gr100.biz.kit.StrKit;
import com.jfinal.log.Log;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * Created by YFsoft on 16/3/14.
 */

@Controller
public class OrderController extends ApiController{

    private static final Log logger = Log.getLog(OrderController.class);

    OrderService orderService = new OrderService();

    @ResponseBody
    @RequestMapping(value= "v1.0/order/create",method= RequestMethod.POST)
    public Map create(@RequestParam Map args) {
        String type = StrKit.getStringValue(args,"type");
        String toProvince = StrKit.getStringValue(args,"toProvince");
        String toCity = StrKit.getStringValue(args,"toCity");
        String toName = StrKit.getStringValue(args,"toName");
        String toPhone = StrKit.getStringValue(args,"toPhone");
        String toStreet = StrKit.getStringValue(args,"toStreet");
        String total = StrKit.getStringValue(args,"total");
        String finalAmount = StrKit.getStringValue(args,"finalAmount");
        String discount = StrKit.getStringValue(args,"discount");
        int gCoinChecked = StrKit.getIntValue(args,"gCoinChecked");
        String gCoin = StrKit.getStringValue(args,"gCoin");
        String remark = StrKit.getStringValue(args,"remark");
        String areaName = StrKit.getStringValue(args,"areaName");
        String shopName = StrKit.getStringValue(args,"shopName");
        String uid = StrKit.getStringValue(args,"uid");
        String couponId = StrKit.getStringValue(args,"couponId");
        String par = StrKit.getStringValue(args,"par");
        String bookdate = StrKit.getStringValue(args,"bookdate");
        String expresscost = StrKit.getStringValue(args,"expresscost");
        String expressname = StrKit.getStringValue(args,"expressname");
        String expresstype = StrKit.getStringValue(args,"expresstype");
        String deliveryTime = StrKit.getStringValue(args,"deliveryTime");
        int UserTimingCouponId = StrKit.getIntValue(args,"UserTimingCouponId");
        int MemberTimingCouponId = StrKit.getIntValue(args,"MemberTimingCouponId");
        String TimingPrice = StrKit.getStringValue(args,"TimingPrice");
        String mj_title = StrKit.getStringValue(args,"mj_title");
        String mj_amount = StrKit.getStringValue(args,"mj_amount");
        String couponDiscountId = StrKit.getStringValue(args,"couponDiscountId");
        String couponDiscountPar = StrKit.getStringValue(args,"couponDiscountPar");

        //特殊处理 中文转码
        toProvince = StrKit.checkNull(StrKit.checkNull(toProvince,null),"江苏省");
        toCity = StrKit.checkNull(StrKit.checkNull(toCity,null),"扬州市");

        toName = StrKit.checkNull(toName);
        toPhone = StrKit.checkNull(toPhone);
        toStreet = StrKit.checkNull(toStreet);
        remark = StrKit.checkNull(remark);
        areaName = StrKit.checkNull(areaName);
        shopName = StrKit.checkNull(shopName);
        expressname = StrKit.checkNull(expressname);
        expresstype = StrKit.checkNull(expresstype);
        deliveryTime = StrKit.checkNull(deliveryTime,"0");
        mj_title = StrKit.checkNull(mj_title);

        Map map = orderService.createOrder(type,toProvince,toCity,toName,toPhone,toStreet,total,finalAmount,discount,gCoinChecked,gCoin,remark,areaName,shopName,uid,couponId,par,bookdate,expresscost,expressname,expresstype,deliveryTime,UserTimingCouponId,MemberTimingCouponId,TimingPrice,mj_title,mj_amount,couponDiscountId,couponDiscountPar);
        return parseResult(map);
    }

    

    @ResponseBody
    @RequestMapping(value= "v1.0/order/pay",method= RequestMethod.POST)
    public Map pay(@RequestParam Map args) {
        String orderid = args.get("orderid").toString();
        double amount = Double.parseDouble(args.get("amount").toString());
        String payway = args.get("payway").toString();

        StrKit.checkNull(StrKit.checkNull(payway,null),"货到付款");

        Map map = orderService.pay(orderid, amount, payway);
        return parseResult(map);
    }

    @ResponseBody
    @RequestMapping(value= "v1.0/order/notify",method= RequestMethod.POST)
    public Map notify(@RequestParam Map args){
        String orderid = args.get("orderid").toString();
        double amount = Double.parseDouble(args.get("amount").toString());
        String payway = args.get("payway").toString();

        StrKit.checkNull(StrKit.checkNull(payway,null),"货到付款");

        Map map = orderService.notify(orderid, amount, payway);
        return parseResult(map);
    }

    @ResponseBody
    @RequestMapping(value= "v1.0/order/recharge",method= RequestMethod.POST)
    public Map recharge(@RequestParam Map args){
        String uid = args.get("uid").toString();
        double amount = Double.parseDouble(args.get("amount").toString());
        Map map = orderService.recharge(uid, amount);
        return parseResult(map);
    }

    @ResponseBody
    @RequestMapping(value= "v1.0/order/rechargenotify",method= RequestMethod.POST)
    public Map rechargenotify(@RequestParam Map args) {
        String orderid = args.get("orderid").toString();
        double amount = Double.parseDouble(args.get("amount").toString());
        Map map = orderService.rechargenotify(orderid, amount);
        return parseResult(map);
    }
}
